package com.quick.controller;

import com.quick.domain.Codes;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author jojo.wang
 * @title: GlobalExceptionHandler
 * @projectName spring-boot-quick
 * @description: TODO
 * @date 2019/5/615:14
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //不满足@RequiresGuest注解时抛出的异常信息
    private static final String GUEST_ONLY = "Attempting to perform a guest-only operation";
    @ExceptionHandler(ShiroException.class)
    public String handleShiroException(ShiroException e) {
        String eName = e.getClass().getSimpleName();
        log.error("shiro执行出错：{}",eName);
        return "鉴权或授权过程出错";
    }

    @ExceptionHandler(UnauthenticatedException.class)
    public String page401(UnauthenticatedException e) {
        String eMsg = e.getMessage();
        if (StringUtils.startsWithIgnoreCase(eMsg,GUEST_ONLY)){
            return  "只允许游客访问，若您已登录，请先退出登录";
        }else{
            return "用户未登录";
        }
    }

    @ExceptionHandler(UnauthorizedException.class)
    public String page403() {
        return "用户没有访问权限";
    }
}
